
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import unittest
from unint_2.Mysql import Mysql
from selenium.webdriver.common.keys import Keys

class A3mall(unittest.TestCase):

    def setUp(self) -> None:
        print('start case')
        self.driver = webdriver.Edge()
        # self.driver.maximize_window()
        self.driver.get('http://10.59.9.14:7003/')
        self.db = Mysql(host='10.59.9.14',user='root',passwoord='yidong666',database='a3mall')

    def tearDown(self) -> None:
        time.sleep(6)
        self.driver.quit()
        print('end case')


    def test_A3mall_01(self):
        #点击登录
        self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
        #输入用户名和密码
        self.driver.find_element(By.NAME, 'username').send_keys('15077899457')
        self.driver.find_element(By.NAME, 'password').send_keys('111111')
        time.sleep(15)
        #点击登录按钮
        self.driver.find_element(By.CSS_SELECTOR, '.finish-btn').click()
        time.sleep(4)
        #断言页面用户名与数据库的用户名
        dy1 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[1]').text
        print(dy1)
        resulf = self.db.chaxun(f'select nickname from mall_users where mobile={15077899457}')
        print(resulf[0][0])
        self.assertIn(resulf[0][0],dy1)
        #断言登录金额
        dy2 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[4]/span').text
        print(dy2)
        numdy2 = dy2.split()[1]
        print(numdy2)
        resulf1 = self.db.chaxun(f'select amount from mall_users where mobile={15077899457}')
        print(resulf1[0][0])
        # print(resulf1[0][1])
        # unm = resulf1[0][0]
        # print(type(unm))
        self.assertEqual(str(resulf1[0][0]),numdy2)
        #断言积分
        dy3 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[2]/span[1]').text
        print(dy3.split()[0])
        numdy3 = dy3.split()[0]
        resulf2 = self.db.chaxun(f'select point from mall_users where mobile={15077899457}')
        print(resulf2[0][0])
        self.assertTrue(str(resulf2[0][0])==numdy3)
        #首页
        self.driver.find_element(By.LINK_TEXT,'首页').click()
        #积分商城
        self.driver.find_element(By.XPATH,'//a[@href="/home/point/index.html"]').click()
        time.sleep(3)
        #断言积分列表的商品名称和价格
        dy4 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[1]/ul/li[2]/div[1]/em/a').text
        print(dy4)
        dy5 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[1]/ul/li[2]/div[1]/p').text
        print(dy5)
        numdy5 = dy5.replace(' 积分','')
        a = self.db.chaxun(f"select sell_price from mall_goods where title='{dy4}'")
        print(a)
        self.assertEqual(float(a[0][0]),float(numdy5))
        #商品详情
        self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[1]/ul/li[2]/div[1]/span/a/img').click()
        #商品详情商品名称和商品价格
        dy6 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div/div[2]/form/div/div[1]/span').text
        print(dy6)
        dy7 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div/div[2]/form/div/div[3]/div[1]/div[2]').text
        print(dy7)
        time.sleep(2)
        self.driver.find_element(By.ID,'buy-cart').click()
        time.sleep(2)
        dy8 = self.driver.find_element(By.XPATH,'/html/body/div[4]/div[1]/a[2]').text
        print(dy8)
        assert '确认订单'==dy8
        #下滑
        self.driver.execute_script('window.scrollTo(0,500)')
        time.sleep(2)
        dy9 = self.driver.find_element(By.ID,'order-amount').text
        print(dy9)
        #点击下一步
        self.driver.find_element(By.ID,'order-button').click()
        time.sleep(2)
        dy10 = self.driver.find_element(By.XPATH,'/html/body/div[4]/div[1]/a[2]').text
        print(dy10)
        assert '订单完成'==dy10
        resulf4 = self.db.chaxun(f'select address from mall_users_address where mobile={15077899457}')
        print(resulf4)
        #断言收货地址
        dy11 = self.driver.find_element(By.XPATH,'/html/body/div[4]/div[5]/div[2]/ul/li[3]').text
        print(dy11)
        #
        self.assertIn()



if __name__ == '__main__':
    unittest.main()


